package com.hjm.admin.moule.viewmodel.fragment;

import android.content.Context;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.hjm.admin.moule.api.BookService;
import com.hjm.admin.moule.model.BookClassifyBean;
import com.hjm.admin.moule.utils.NetworkUtils;
import com.hjm.admin.moule.utils.rxhelper.RxObserver;
import com.hjm.admin.moule.view.fragment.IClassifyBook;
import com.hjm.admin.moule.viewmodel.BaseViewModel;

public class VMBookClassify extends BaseViewModel {
    IClassifyBook iClassifyBook;

    public VMBookClassify(Context mContext, IClassifyBook iClassifyBook) {
        super(mContext);
        this.iClassifyBook = iClassifyBook;
    }

    public void bookClassify() {
        if (!NetworkUtils.isConnected()) {
            if (iClassifyBook != null) {
                iClassifyBook.netWorkError();
            }
            return;
        }
        RxHttpUtils.getInstance().createApi(BookService.class)
                .bookClassify()
                .compose(Transformer.switchSchedulers())
                .subscribe(new RxObserver<BookClassifyBean>() {
                    @Override
                    protected void onError(String errorMsg) {
                        if (iClassifyBook != null) {
                            iClassifyBook.stopLoading();
                            iClassifyBook.errorData(errorMsg);
                        }
                    }

                    @Override
                    protected void onSuccess(BookClassifyBean data) {
                        if (iClassifyBook != null) {
                            iClassifyBook.stopLoading();
                            if (data == null) {
                                iClassifyBook.emptyData();
                                return;
                            }
                            iClassifyBook.getBookClassify(data);
                        }
                    }
                });
    }
}
